#include <iostream>
#include <string>
#include <new>
using namespace std;
const int BUF = 512;

class JustTesting  
{
private:
    string words;
    int number;
public:
    JustTesting(const string & s = "Just Testing", int n = 0)
    {words = s; number = n; cout << words << " constructed\n";};
    ~JustTesting(){cout << words << " destroyed\n";};
    void Show() const {cout << words << " , " << number << endl;}
};

int main_placenew1()
{   
    char *buf = new char[BUF];
    
    JustTesting *pc1, *pc2;
    pc1 = new (buf)JustTesting;
    pc2 = new JustTesting("Heap1", 20);
    cout << "Memory block address:\n" 
         << "buffer: " << (void *)buf
         << "  heap:" << pc2 << endl;
    cout << "Memory contents:\n";
    cout << pc1 << ": ";
    pc1->Show();
    cout << pc2 << ": ";
    pc2->Show();

    JustTesting *pc3, *pc4;
    pc3 = new (buf + sizeof (JustTesting))JustTesting("Bad Idea", 6);
    pc4 = new JustTesting("Heap2", 10);

    cout << "Memory contents----:\n";
    cout << pc1 << ": ";
    pc1->Show();
    cout << pc3 << ": ";
    pc3->Show();
    cout << pc4 << ": ";
    pc4->Show();

    delete pc2;
    delete pc4;
    pc3->~JustTesting();
    pc1->~JustTesting();
    // 内存实际上是被释放掉了，但是不会掉用析构函数，必须自己显式调用
    delete [] buf; 
    cout << "Done\n";
    return 0;
}

